﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using ZFine.Code;
using ZFine.Data;
using ZFine.Domain.Entity;

namespace Logic.Repository.Project
{
    public class PersonSalaryRepository : RepositoryBase<PersonalSalaryEntity>, IPersonSalaryRepository
    {
        public PersonalSalaryEntity FindEntity(Expression<Func<PersonalSalaryEntity, bool>> predicate)
        {
            return dbcontext.Set<PersonalSalaryEntity>().Include(f => f.OperationGudaoCarEntity).Include(f => f.SalaryEntity).Include(f => f.Persion).Include(f => f.EveryCarID).Include(f => f.BeforePersonalSalary).FirstOrDefault(predicate);
        }

        public int Insert(PersonalSalaryEntity entitys)
        {

            if (entitys.OperationGudaoCarEntity != null)
            {
                dbcontext.Entry<OperationGudaoCarEntity>(entitys.OperationGudaoCarEntity).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.OperationGudaoCarEntity).CurrentValue = entitys.OperationGudaoCarEntity;
                //dbcontext.Set<CourseEntity>().Attach(entitys).CourseType = entitys.CourseType;
            }

            if (entitys.SalaryEntity != null)
            {
                dbcontext.Entry<SalaryEntity>(entitys.SalaryEntity).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.SalaryEntity).CurrentValue = entitys.SalaryEntity;
                //dbcontext.Set<CourseEntity>().Attach(entitys).CourseTime = entitys.CourseTime;
            }

            if (entitys.Persion != null)
            {
                dbcontext.Entry<UserEntity>(entitys.Persion).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.Persion).CurrentValue = entitys.Persion;
                //dbcontext.Set<CourseEntity>().Attach(entitys).PublicName = entitys.PublicName;
            }
            if (entitys.EveryCarID != null)
            {
                dbcontext.Entry<CarriageEntity>(entitys.EveryCarID).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.EveryCarID).CurrentValue = entitys.EveryCarID;
                //dbcontext.Set<CourseEntity>().Attach(entitys).PublicName = entitys.PublicName;
            }
            if (entitys.BeforePersonalSalary != null)
            {
                dbcontext.Entry<PersonalSalaryEntity>(entitys.BeforePersonalSalary).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.BeforePersonalSalary).CurrentValue = entitys.BeforePersonalSalary;
                //dbcontext.Set<CourseEntity>().Attach(entitys).PublicName = entitys.PublicName;
            }
            dbcontext.Entry<PersonalSalaryEntity>(entitys).State = EntityState.Unchanged;
            return dbcontext.SaveChanges();
            //return dbcontext.SaveChanges();
        }

        public int InsertPer(List<PersonalSalaryEntity> entitys,OperationGudaoCarEntity oper)
        {
            dbcontext.Entry<OperationGudaoCarEntity>(oper).State = EntityState.Added;
            if (oper.OperationPersion != null)
            {
                dbcontext.Entry<UserEntity>(oper.OperationPersion).State = EntityState.Unchanged;
                dbcontext.Entry(oper).Reference(f => f.OperationPersion).CurrentValue = oper.OperationPersion;
            }
            if (oper.SalaryPeople != null)
            {
                dbcontext.Entry<UserEntity>(oper.SalaryPeople).State = EntityState.Unchanged;
                dbcontext.Entry(oper).Reference(f => f.SalaryPeople).CurrentValue = oper.SalaryPeople;
                //dbcontext.Set<CourseEntity>().Attach(entitys).CourseTime = entitys.CourseTime;
            }
            if (oper.FaHuoYuan != null)
            {
                dbcontext.Entry<UserEntity>(oper.FaHuoYuan).State = EntityState.Unchanged;
                dbcontext.Entry(oper).Reference(f => f.FaHuoYuan).CurrentValue = oper.FaHuoYuan;
            }
            //if (oper.WorkPlan != null)
            //{
            //    dbcontext.Entry<WorkPlanEntity>(oper.WorkPlan).State = EntityState.Unchanged;
            //    dbcontext.Entry(oper).Reference(f => f.WorkPlan).CurrentValue = oper.WorkPlan;
            //}
            dbcontext.SaveChanges();
            dbcontext.Entry<OperationGudaoCarEntity>(oper).State = EntityState.Unchanged;

            foreach (var entity in entitys)
            {
                entity.OperationGudaoCarEntity = oper;
                if (entity.OperationGudaoCarEntity != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(entity.OperationGudaoCarEntity).State = EntityState.Unchanged;
                    dbcontext.Entry(entity).Reference(f => f.OperationGudaoCarEntity).CurrentValue = entity.OperationGudaoCarEntity;
                }

                if (entity.SalaryEntity != null)
                {
                    dbcontext.Entry<SalaryEntity>(entity.SalaryEntity).State = EntityState.Unchanged;
                    dbcontext.Entry(entity).Reference(f => f.SalaryEntity).CurrentValue = entity.SalaryEntity;
                }

                if (entity.Persion != null)
                {
                    if (oper.OperationPersion != null && entity.Persion.F_Id == entity.OperationGudaoCarEntity.OperationPersion.F_Id)
                    {
                        entity.Persion = oper.OperationPersion;
                        //dbcontext.Entry(entity).Reference(f => f.Persion).CurrentValue = oper.OperationPersion;
                        dbcontext.Entry<UserEntity>(entity.Persion).State = EntityState.Unchanged;
                        dbcontext.Entry(entity).Reference(f => f.Persion).CurrentValue = entity.OperationGudaoCarEntity.OperationPersion;
                        //if (entity.OperationGudaoCarEntity != null && entity.OperationGudaoCarEntity.OperationPersion != null)
                        //{
                        //    if (entity.Persion.F_Id == entity.OperationGudaoCarEntity.OperationPersion.F_Id)
                        //    {
                        //        dbcontext.Entry(entity).Reference(f => f.OperationGudaoCarEntity.OperationPersion).CurrentValue = entity.Persion;
                        //        //entity.OperationGudaoCarEntity.OperationPersion = entity.Persion;
                        //        dbcontext.Entry<UserEntity>(entity.OperationGudaoCarEntity.OperationPersion).State = EntityState.Unchanged;
                        //    }
                        //}
                    }
                    else
                    {
                        dbcontext.Entry<UserEntity>(entity.Persion).State = EntityState.Unchanged;
                        dbcontext.Entry(entity).Reference(f => f.Persion).CurrentValue = entity.Persion;
                    }


                }
                if (entity.EveryCarID != null)
                {
                    dbcontext.Entry<CarriageEntity>(entity.EveryCarID).State = EntityState.Unchanged;
                    dbcontext.Entry(entity).Reference(f => f.EveryCarID).CurrentValue = entity.EveryCarID;
                }
                if (entity.BeforePersonalSalary != null)
                {
                    dbcontext.Entry<PersonalSalaryEntity>(entity.BeforePersonalSalary).State = EntityState.Unchanged;
                    dbcontext.Entry(entity).Reference(f => f.BeforePersonalSalary).CurrentValue = entity.BeforePersonalSalary;
                }
                dbcontext.Entry<PersonalSalaryEntity>(entity).State = EntityState.Added;
               
            }

            return dbcontext.SaveChanges(); ;
        }

        public List<PersonalSalaryEntity> FindList(string strSql)
        {
            return dbcontext.Database.SqlQuery<PersonalSalaryEntity>(strSql).ToList<PersonalSalaryEntity>();
        }


        public List<PersonalSalaryEntity> FindList(Expression<Func<PersonalSalaryEntity, bool>> predicate, Pagination pagination)
        {
            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
            string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;
            var tempData = dbcontext.Set<PersonalSalaryEntity>().Include(f => f.OperationGudaoCarEntity).Include(f => f.SalaryEntity).Include(f => f.Persion).Include(f => f.EveryCarID).Include(f => f.BeforePersonalSalary).Where(predicate);
            foreach (string item in _order)
            {
                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
                string[] _orderArry = _orderPart.Split(' ');
                string _orderField = _orderArry[0];
                bool sort = isAsc;
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }
                var parameter = Expression.Parameter(typeof(PersonalSalaryEntity), "t");
                var property = typeof(PersonalSalaryEntity).GetProperty(_orderField);
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(PersonalSalaryEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<PersonalSalaryEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<PersonalSalaryEntity>(pagination.rows * (pagination.page - 1)).Take<PersonalSalaryEntity>(pagination.rows).AsQueryable();
            return tempData.ToList();
        }
    }
}
